/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.controller;

import com.xy.biz.uc.domain.bo.RoleBO;
import com.xy.biz.uc.domain.converter.RoleConverter;
import com.xy.biz.uc.domain.dto.RoleDTO;
import com.xy.biz.uc.domain.enums.RoleTypeEnum;
import com.xy.biz.uc.domain.query.RoleIdQuery;
import com.xy.biz.uc.domain.query.RoleQuery;
import com.xy.biz.uc.domain.req.GetRoleRequest;
import com.xy.biz.uc.domain.req.ListRoleItemRequest;
import com.xy.biz.uc.domain.req.ListRoleRequest;
import com.xy.biz.uc.domain.req.SaveRoleRequest;
import com.xy.biz.uc.service.RoleService;
import com.xy.lang.domain.KeyValue;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * 
 * @author zsp
 * @date 2020-8-19
 */
@Slf4j
@RestController
@RequestMapping("/api/uc/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveRole(@RequestBody SaveRoleRequest request) {
		roleService.saveRole(RoleConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateRole(@RequestBody SaveRoleRequest request) {
		roleService.updateRole(RoleConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteRole(@RequestBody GetRoleRequest request) {
		roleService.deleteRole(request.getRoleId());
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/get")
	public Result<RoleDTO> getRole(@RequestBody GetRoleRequest request) {
		return Result.success(RoleConverter.toDTO(roleService.getRole(RoleIdQuery.builder().build().addRoleId(request.getRoleId()))));
	}

	@PostMapping(value = "/list_by_id")
	public Result<List<RoleDTO>> listRoleById(@RequestBody Collection<Long> collection) {
		return Result.success(RoleConverter.toDTO(roleService.listRoleById(RoleIdQuery.builder().roleIdList(new ArrayList<>(collection)).build())));
	}

	@PostMapping(value = "/list")
	public Result<List<RoleDTO>> listRole(@RequestBody ListRoleRequest request) {
		return Result.success(RoleConverter.toDTO(roleService.listRole(RoleConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	public Result<Integer> countRole(@RequestBody ListRoleRequest request) {
		return Result.success(roleService.countRole(RoleConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	public Result<Page<RoleDTO>> pageRole(@RequestBody ListRoleRequest request) {
		RoleQuery query = RoleConverter.toQuery(request);
		List<RoleDTO> list = RoleConverter.toDTO(roleService.listRole(query));
		int count = roleService.countRole(query);
		return Result.success(new Page<>(list, count));
	}

	@Validatable
	@PostMapping(value = "/list_resource_id")
	public Result<List<Long>> listResourceIdByRoleId(@RequestBody GetRoleRequest request) {
		return Result.success(roleService.listResourceIdByRoleId(request.getRoleId()));
	}

	@PostMapping(value = "/list_role_type")
	public Result<List<KeyValue<Integer, String>>> listRoleType() {
		return Result.success(
				Arrays.stream(RoleTypeEnum.values())
						.map(e -> new KeyValue<>(e.getCode(), e.getDesc()))
						.collect(Collectors.toList()));
	}

	@PostMapping(value = "/list_item")
	public Result<List<KeyValue<Long, String>>> listRoleItem(@RequestBody ListRoleItemRequest request) {
		RoleQuery query = RoleQuery.max().typeList(request.getTypeList()).build();
		List<RoleBO> roleBOList = roleService.listRole(query);
		if(!CollectionUtils.isEmpty(roleBOList)) {
			return Result.success(
					roleBOList.stream().map(e -> new KeyValue<>(e.getRoleId(), e.getName())).collect(Collectors.toList()));
		}
		return Result.success(Collections.emptyList());
	}
	
}
